package com.tdam2011.grupo02.ui;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.domain.ContactLocation;
import com.tdam2011.grupo02.support.ContactOverlay;
import com.tdam2011.grupo02.support.LocationUpdateInterface;
import com.tdam2011.grupo02.support.MapLocationController;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;

public class ContactMapActivity extends MapActivity implements
		LocationUpdateInterface {

	private MapLocationController locationController;
	private List<Overlay> mapOverlays;
	private boolean isLocated = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_map);
		
		Bundle bundle = getIntent().getExtras();
		double latitude = bundle.getDouble("latitude");
		double longitude = bundle.getDouble("longitude");
		String address = bundle.getString("address");
		
	//	locationController = new MapLocationController(this, this);

		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		mapOverlays = mapView.getOverlays();		
		
		updateContactLocation(latitude, longitude, address);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void updateLocation(Location location) {
		if (location != null && !isLocated) {
		/*	isLocated = true;

			GeoPoint point = null;
			Double latitude = location.getLatitude() * 1E6;
			Double longitude = location.getLongitude() * 1E6;

			point = new GeoPoint(latitude.intValue(), longitude.intValue());

			Log.d("LocationDemo", "GeoPoint: " + point.getLatitudeE6() + " - "
					+ point.getLongitudeE6());

			Drawable drawable = this.getResources().getDrawable(
					R.drawable.icon_app);
			ContactOverlay itemizedoverlay = new ContactOverlay(drawable, this);

			OverlayItem overlayitem = new OverlayItem(point, "Hi!",
					"I'm in somewhere!");

			itemizedoverlay.addOverlay(overlayitem);
			mapOverlays.add(itemizedoverlay); 
			*/
		}
	}
	
	private void updateContactLocation(double latitude, double longitude, String address) {
		GeoPoint point = null;
		Double latitudeAux = latitude * 1E6;
		Double longitudeAux = longitude * 1E6;

		point = new GeoPoint(latitudeAux.intValue(), longitudeAux.intValue());

		Drawable drawable = this.getResources().getDrawable(
				R.drawable.location_map_icon_medium);
		ContactOverlay itemizedoverlay = new ContactOverlay(drawable, this);

		OverlayItem overlayitem = new OverlayItem(point, "contact name",
				address);

		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay);
	}
}
